package swe.article;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class ArticleDAO implements DAO<Article> {

	String filename;

	public ArticleDAO(String filename) {
		this.filename = filename;
	}

	@Override
	public void save(List<Article> article) {
		try {

			ObjectOutputStream os = new ObjectOutputStream(
					new FileOutputStream(filename));
			for (int i = 0; i < article.size(); i++) {
				os.writeObject(article.get(i));
			}
			os.flush();
			os.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public List<Article> read() {
		List<Article> articleList = new ArrayList<Article>();
		Article article = null;

		try {
			ObjectInputStream is = new ObjectInputStream(new FileInputStream(
					filename));

			while ((article = (Article) is.readObject()) != null) {
				articleList.add(article);
			}

			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return articleList;
	}
	
	public void add(Article article) {
		
	}

}